#include "SceneManager.h"

#include "RenderDevice.h"

#include <d3d9.h>
#include <d3dx9.h>

//////////////////////////////////////////////////
//SceneManager
//////////////////////////////////////////////////

//------------------------------------------------

SceneManager::SceneManager(void)
: m_loaded(false)
  {
  }

//------------------------------------------------

SceneManager::~SceneManager(void)
  {
  }

//------------------------------------------------

void SceneManager::LoadScene(std::string i_path, std::string i_name, RenderDevice* i_render_device)
  {
  D3DXVECTOR3 position(0.0f, 5.0f, -15.0f);
	D3DXVECTOR3 target(0.0, 5.0f, -16.0f);
	D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
  D3DXMATRIX V;
	D3DXMatrixLookAtLH(&V, &position, &target, &up);
  i_render_device->GetDirect3DDevice()->SetTransform(D3DTS_VIEW, &V);

	D3DXMATRIX proj;
	D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI * 0.5f, (float)800 / (float)600, 1.0f, 1000.0f);
	i_render_device->GetDirect3DDevice()->SetTransform(D3DTS_PROJECTION, &proj);

  i_render_device->GetDirect3DDevice()->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

  i_render_device->GetDirect3DDevice()->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);	
	
	i_render_device->GetDirect3DDevice()->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
	i_render_device->GetDirect3DDevice()->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
	i_render_device->GetDirect3DDevice()->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESSEQUAL);

  // Create the sun light source.
	D3DLIGHT9 sun;
	sun.Type = D3DLIGHT_DIRECTIONAL;
	sun.Diffuse.r = 0.5f;
	sun.Diffuse.g = 0.5f;
	sun.Diffuse.b = 0.5f;
	sun.Diffuse.a = 1.0f;
	sun.Specular = sun.Diffuse;
	sun.Ambient.r = 0.3f;
	sun.Ambient.g = 0.3f;
	sun.Ambient.b = 0.3f;
	sun.Ambient.a = 1.0f;
	sun.Direction = D3DXVECTOR3( -0.6f, -0.3f, 0.4f);

	// Switch lighting on, enable the sun light, and specular highlights.
	i_render_device->GetDirect3DDevice()->SetRenderState( D3DRS_LIGHTING, true );
	i_render_device->GetDirect3DDevice()->SetLight( 0, &sun );
	i_render_device->GetDirect3DDevice()->LightEnable( 0, true );
	i_render_device->GetDirect3DDevice()->SetRenderState( D3DRS_SPECULARENABLE, true );




  LPD3DXBUFFER materialBuffer;  

  std::string filename = i_path + i_name;

  // Load the mesh from the specified file
  D3DXLoadMeshFromX(filename.c_str(), D3DXMESH_SYSTEMMEM, i_render_device->GetDirect3DDevice(), NULL, &materialBuffer, NULL, &m_numMaterials, &m_mesh);

  D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)materialBuffer->GetBufferPointer();  
  meshTextures  = new LPDIRECT3DTEXTURE9[m_numMaterials];

  meshMaterials = new D3DMATERIAL9[m_numMaterials];  

  for (DWORD i=0; i<m_numMaterials; i++)
    {
    // Copy the material
    meshMaterials[i] = d3dxMaterials[i].MatD3D;
    // Set the ambient color for the material (D3DX does not do this)
    meshMaterials[i].Ambient = meshMaterials[i].Diffuse;   
    }
  materialBuffer->Release();  
  m_loaded = true;
  }

//------------------------------------------------

void SceneManager::RenderScene(RenderDevice* i_render_device)
  {  
  for (DWORD i=0; i<m_numMaterials; i++)
    {    
    // Set the material and texture for this subset
    i_render_device->GetDirect3DDevice()->SetMaterial(&meshMaterials[i]);   
    //i_render_device->GetDirect3DDevice()->SetTexture(0,0);
    // Draw the mesh subset
    m_mesh->DrawSubset(i);
    meshTextures[i] = NULL;  
    }  
  }

//------------------------------------------------

bool SceneManager::IsLoaded()
  {
  return m_loaded;
  }

//------------------------------------------------

//////////////////////////////////////////////////